package com.gjzhao.fmmall.config;

import com.gjzhao.fmmall.interceptor.CheckTokenInterceptor;
import com.gjzhao.fmmall.interceptor.RenewalInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description: 拦截器配置
 * @Author: gjzhao
 * @Date Created in 2021-12-31 10:53 下午
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    CheckTokenInterceptor CheckTokenInterceptor;
    @Autowired
    RenewalInterceptor renewalInterceptor;

    /**
     * 将拦截器配置交由spring进行管理
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //拦截器1：拦截受限资源的访问
        registry.addInterceptor(CheckTokenInterceptor)
                //.addPathPatterns("/**") //理应是所有请求
                .addPathPatterns("/orders/**") //订单模块
                .addPathPatterns("/userAddr/**") //收货地址模块
                .addPathPatterns("/shoppingCart/**") //购物车模块
                .excludePathPatterns("/user/**"); //todo 测试方便 排除哪些路径不进行拦截

        //拦截器2：拦截所有资源的访问请求(在登录状态下，访问非受限资源需要虚命操作，所以访问非受限资源一定要带上token)
        registry.addInterceptor(renewalInterceptor)
                .addPathPatterns("/**"); //拦截所有请求
    }
}
